package com.qiuling.iot.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.qiuling.iot.constants.SMSConstants;
import com.qiuling.iot.entity.SMSObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author：zhangqiang
 * @Description：短信发送工具类
 * @Date：Created in 16:03 2020/3/11
 * @Modified By：
 */
@Slf4j
public class SMSUtils {

    /**
     * @description 获取球星参数
     * </br>       
     * @param phone
     * @param code
     * @return com.aliyuncs.CommonRequest
     * @author zhangqiang
     * @date 16:45 2020/3/11
     */
    public static CommonRequest getCommonRequest (String phone, String code) {
        CommonRequest request = new CommonRequest();
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        String value = JSON.toJSONString(map);
        request.setMethod(MethodType.POST);
        request.setDomain(SMSConstants.Domain);
        request.setVersion(SMSConstants.Version);
        request.setAction(SMSConstants.Action);
        request.putQueryParameter("RegionId", SMSConstants.RegionId);
        request.putQueryParameter("PhoneNumbers", "15605513492");
        request.putQueryParameter("SignName", SMSConstants.SignName);
        request.putQueryParameter("TemplateCode", SMSConstants.Verification_Template_Code);
        request.putQueryParameter("TemplateParam", value);
        return request;
    }

   /**
    * @description 配置客户端
    * </br>       
    * @param
    * @return com.aliyuncs.IAcsClient
    * @author zhangqiang
    * @date 19:02 2020/3/11
    */
    public static IAcsClient getClient () {
        EncryptUtil instance = EncryptUtil.getInstance();
        String AccessKey_ID = instance.AESdecode(SMSConstants.AccessKey_ID, "AccessKey_ID");
        String Access_Key_Secret = instance.AESdecode(SMSConstants.Access_Key_Secret, "Access_Key_Secret");
        DefaultProfile profile = DefaultProfile.getProfile(SMSConstants.RegionId, AccessKey_ID,
                Access_Key_Secret);
        IAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * @description 发送短信
     * </br>       
     * @param phone
     * @param code
     * @return com.aliyuncs.CommonResponse
     * @author zhangqiang
     * @date 19:50 2020/3/11
     */
    public static CommonResponse sendSMS(String phone, String code) {
        IAcsClient client = getClient();
        CommonRequest request = getCommonRequest(phone, code);
        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
            String data = response.getData();
            Gson gson = new Gson();
            SMSObject obj = gson.fromJson(data, SMSObject.class);

            if(obj.getCode().equals("OK")) {
                log.info("短信发送成功, 验证码：{}", code);
            }else {
                log.error("发送失败，message：{}", obj.getMessage());
            }
        } catch (ClientException e) {
            log.error(e.getMessage());
        }
        return response;
    }

}
